#!/usr/bin/env python

import telnetlib
import threading
import queue

def get_ip_status(ip):
    server = telnetlib.Telnet()
    for port in range(3310,3322):
        try:
            server.open(ip,port)
            print('{0} port {1} is open'.format(ip,port))
        except Exception as err:
            print('{0} port {1} is not open'.format(ip,port))
        finally:
            server.close()

def check_open(q):
    try:
        while True:
            ip = q.get_nowait()
            get_ip_status(ip)
    except queue.Empty as e:
        pass

def readip():
    with open('ip.txt','r') as f:
        line = f.readline().strip()
    return line 

if __name__ == "__main__":
    host = readip()
    q = queue.Queue()
    for ip in host:
        print(q.put(ip))
    
    threads = []
    for i in range(4000):
        t = threading.Thread(target=check_open,args=(q,))
        t.start()
        threads.append(t)

    for t in threads:
        t.join()
